package com.ttmusic.fragments.maintab;

import java.util.List;

import com.ttmusic.activitys.MainActivity;
import com.ttmusic.activitys.R;
import com.ttmusic.dao.ArtistInfoDao;
import com.ttmusic.entity.ArtistInfo;
import com.ttmusic.utils.ChineseToEnglish;
import com.ttmusic.utils.Utils;
import com.ttmusic.view.LetterBar;
import com.ttmusic.view.adapters.ArtistAdapter;
import com.ttmusic.view.listeners.OnTouchingLetterChangedListener;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

/**
 * 歌手列表界面
 * @author Administrator
 *
 */
public class ArtistsFragment extends ListFragment{
	
	private ArtistInfoDao artistDao;
	private View view;
	private LayoutInflater inflater;
	private boolean isABC = true;  //是否显示Letterbar视图
	private LetterBar artistLetterBar;
	private TextView artistDialog;
	private ListView local_artist;
	//艺术家列表
  	private List<ArtistInfo> artistInfoList;
  	private ArtistAdapter artistAdapter;
  	//ListView的footerView和headerView中的歌手数量
  	private TextView tv_listview_itemcount;
//  	private RelativeLayout artist_listcontent;

	public ArtistsFragment() {
		this.artistDao = ArtistInfoDao.getInstance();
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		this.inflater = inflater;
		view = inflater.inflate(R.layout.fragment_artists, container, false);
		return view;
	}
	
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
//		artist_listcontent = (RelativeLayout)view.findViewById(R.id.artist_listcontent);
//		RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
//		lp.setMargins(0, DisplayUtil.dip2px(getContext(), 83), 0, 0);
//		artist_listcontent.setLayoutParams(lp);
		
		//字母快速检索工具栏
		artistLetterBar = (LetterBar)view.findViewById(R.id.artist_letterBar);
		artistDialog = (TextView)view.findViewById(R.id.artist_dialog);
		local_artist = this.getListView();
		
		artistInfoList = artistDao.findAll();
		artistAdapter = new ArtistAdapter(getActivity(), artistInfoList, isABC);
		this.showLetterBar();
		this.initView();
		this.showArtistList();
	}
	
	/**
	 * 展示歌手列表
	 */
	@SuppressLint("InflateParams")
	private void showArtistList() {
		View header = inflater.inflate(R.layout.listheader_footer_item_count, null);
        tv_listview_itemcount = (TextView) header.findViewById(R.id.tv_listview_itemcount);
        tv_listview_itemcount.setText(artistInfoList.size() + " 位歌手");
        local_artist.addHeaderView(header);
        local_artist.addFooterView(header);
        //自定义的listener 简化了android ListView自带的OnItemClickListener复杂的参数
        local_artist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long index) {
				if(index >= 0 && index < artistInfoList.size()) {
					//Toast.makeText(getContext(), "item被点击了", Toast.LENGTH_SHORT).show();
					//点击歌手 查看该歌手的所有歌曲
					ArtistInfo artistInfo = artistInfoList.get((int)index);
					if(MainActivity.musicOfArtistFragment == null)
						MainActivity.musicOfArtistFragment = new MusicOfArtistFragment();
					MainActivity.musicOfArtistFragment.dataChanged(artistInfo);
					Utils.go(getFragmentManager(), ArtistsFragment.this, MainActivity.musicOfArtistFragment, R.id.mainFragmentContainer);
				}
			}
		});
        local_artist.setAdapter(artistAdapter);
    }
	
	/**
	 * 展示字母快速检索工具栏
	 */
	private void showLetterBar() {
        if (isABC) {
        	artistLetterBar.setTextView(artistDialog);
        	artistLetterBar.setOnTouchingLetterChangedListener(new OnTouchingLetterChangedListener() {
				@Override
				public void onTouchingLetterChanged(String letter) {
					int position = artistAdapter.getPositionForSection(letter.charAt(0));
                    if (position != -1) {
                    	local_artist.setSelection(position);
                    }
				}
			});
        	artistLetterBar.setVisibility(View.VISIBLE);
        } else {
        	artistLetterBar.setVisibility(View.INVISIBLE);
    	}
	}
	
	private void initView() {
        if (isABC) {
        	local_artist.setOnScrollListener(new AbsListView.OnScrollListener() {
                @Override
                public void onScrollStateChanged(AbsListView view, int scrollState) {
                    if (view.getCount() != 0) {
                    	//滚动时显示
                        if (scrollState != AbsListView.OnScrollListener.SCROLL_STATE_IDLE) {
                            artistDialog.setVisibility(View.VISIBLE);
                        } else {
                        //不滚动时隐藏
                        	local_artist.postDelayed(new Runnable() {
                                @Override
                                public void run() {
                                    artistDialog.setVisibility(View.GONE);
                                }
                            }, 100);
                        }
                    }
                }
                @Override
                public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
                	ArtistInfo artistInfo = (ArtistInfo) local_artist.getItemAtPosition(firstVisibleItem);
                    if (artistDialog != null && artistInfo != null) {
                    	String dialogStr = "#";
                        if(artistInfo.getArtist() != null && !"".equals(artistInfo.getArtist())) {
                        	dialogStr = ChineseToEnglish.getPingYin(artistInfo.getArtist()).trim().toUpperCase();
                        }
                        artistDialog.setText(dialogStr.charAt(0)+"");
                    }
                }
            });
        }
    }
	
	/**
	 * 本地数据库歌手列表发生了改变
	 */
	public void changeData() {
		artistInfoList = artistDao.findAll();
		artistAdapter.changeData(artistInfoList);
		tv_listview_itemcount.setText(artistInfoList.size() + " 首歌曲");
	}

	@Override
	public void onHiddenChanged(boolean ishidden) {
		if(!ishidden) {
			//改变margin-bottom的值为0  本地歌曲界面的margin-bottom为
//			RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
//			lp.setMargins(0, DisplayUtil.dip2px(getContext(), 83), 0, 0);
//			artist_listcontent.setLayoutParams(lp);
		}
		super.onHiddenChanged(ishidden);
	}
	
	
	
}
